package com.jrx.anytxn.param.transaction.job.billCompare.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.param.transaction.config.AnytxnBatchConfigProperties;
import com.jrx.anytxn.param.transaction.job.billCompare.bean.BillDetilReadBean;
import com.jrx.anytxn.param.transaction.job.billCompare.bean.ThirdBillDetilReadBean;
import com.jrx.anytxn.param.transaction.job.billCompare.step.*;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;


/**
 * 核心对账-txn内部交易流水汇总job
 *
 * @author : zcc
 * 2020/11/30
 */
@Configuration
public class BillDetailReadConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(BillDetailReadConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private AnytxnBatchConfigProperties anytxnBatchConfigProperties;
    @Autowired
    private BillDetailForFKProcessor billDetailForFKProcessor;
    @Autowired
    private BillDetailForFKWriter billDetailForFKWriter;
    @Autowired
    private BillDetailForHKProcessor billDetailForHKProcessor;
    @Autowired
    private BillDetailForHKWriter billDetailForHKWriter;
    @Autowired
    private BillDetailReadListener billDetailReadListener;

    @Bean
    public Job billDetilReadJob() {
        return jobBuilderFactory.get("billDetilReadJob").incrementer(new RunIdIncrementer()).listener(billDetailReadListener)
                .start(billDetilForFKReadStep())
                .next(billDetilForHKReadStep())
                .build();
    }

    @Bean
    public Step billDetilForFKReadStep() {
        return stepBuilderFactory.get("billDetilForFKReadStep")
                .<TlDisbursementLog, ThirdBillDetilReadBean>chunk(anytxnBatchConfigProperties.getBatchBillCompareConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(billDetailForFKReader())
                .processor(billDetailForFKProcessor)
                .writer(billDetailForFKWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchConfigProperties.getBatchBillCompareConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlDisbursementLog> billDetailForFKReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new BillDetailForFKReader(sqlSessionFactory, org,anytxnBatchConfigProperties.getBatchBillCompareConfiguration().getPageSize());
    }

    @Bean
    public Step billDetilForHKReadStep() {
        return stepBuilderFactory.get("billDetilForHKReadStep")
                .<TlPaymentLog, BillDetilReadBean>chunk(anytxnBatchConfigProperties.getBatchBillCompareConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(billDetailForHKReader())
                .processor(billDetailForHKProcessor)
                .writer(billDetailForHKWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchConfigProperties.getBatchBillCompareConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlPaymentLog> billDetailForHKReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new BillDetailForHKReader(sqlSessionFactory, org,anytxnBatchConfigProperties.getBatchBillCompareConfiguration().getPageSize());
    }


}
